package com.hannesdorfmann.mosby3.mvi.integrationtest.slice;

import com.hannesdorfmann.mosby3.mvi.MviAbilitySlice;
import com.hannesdorfmann.mosby3.mvi.integrationtest.ResourceTable;
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestPresenter;
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestView;
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.fraction.backstack.FirstBackstackMviLifecycleFraction;
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.fraction.backstack.MviLifecycleBackstackAbility;
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.fraction.backstack.SecondBackstackMviLifecycleFraction;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;

import java.util.Optional;

public class MviLifecycleBackstackAbilitySlice extends MviAbilitySlice<LifecycleTestView, LifecycleTestPresenter> implements LifecycleTestView {

    private static volatile MviLifecycleBackstackAbility currentInstance;
    private static final String FIRST_TAG = "firstFragment";
    private static final String SECOND_TAG = "secondFragment";
    public LifecycleTestPresenter presenter;
    public static int createPresenterInvokations = 0;
    public static volatile MviLifecycleBackstackAbilitySlice slice;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
//        super.setMainRoute(MviLifecycleBackstackAbilitySlice.class.getName());
        super.setUIContent(ResourceTable.Layout_ability_backstack_mvi_container);
        currentInstance = getAbilityInstance();
        slice = getInstance();
    }

    public static MviLifecycleBackstackAbility getAbilityInstance(){
        if(MviLifecycleBackstackAbilitySlice.currentInstance == null){
            if(getInstance() != null){
                MviLifecycleBackstackAbilitySlice.currentInstance = (MviLifecycleBackstackAbility) getInstance().getAbility();
            }
        }
        return  MviLifecycleBackstackAbilitySlice.currentInstance;
    }
    public static MviLifecycleBackstackAbilitySlice getInstance(){
        if(MviLifecycleBackstackAbilitySlice.slice == null){
            MviLifecycleBackstackAbilitySlice.slice = new MviLifecycleBackstackAbilitySlice();
        }
        return  MviLifecycleBackstackAbilitySlice.slice;
    }

    public FirstBackstackMviLifecycleFraction getFirstFragment() {
        Optional<Fraction> fractionOptional = currentInstance.getFractionManager().getFractionByTag(FIRST_TAG);
        return (FirstBackstackMviLifecycleFraction) fractionOptional.get();

    }

    public SecondBackstackMviLifecycleFraction getSecondFragment() {
        Optional<Fraction> fractionOptional = currentInstance.getFractionManager().getFractionByTag(FIRST_TAG);
        return (SecondBackstackMviLifecycleFraction) fractionOptional.get();

    }

    public void putSecondFragmentOnTopOfBackStack() {
        currentInstance.getFractionManager()
                .startFractionScheduler()
                .replace(ResourceTable.Id_stackContainer, new SecondBackstackMviLifecycleFraction())
                .submit();
    }

    @Override protected void onStop() {
        super.onStop();
        System.err.println(getClass().getSimpleName()+ " onDestroy() " + this);
        currentInstance = null;
    }

    @Override public LifecycleTestPresenter createPresenter() {
        createPresenterInvokations++;
        presenter = new LifecycleTestPresenter();
        System.err.println(getClass().getSimpleName()+ " createPresenter() " + this + " " + presenter);
        return presenter;
    }


    public static void pressBackButton() {
        currentInstance.getUITaskDispatcher().syncDispatch(new Runnable() {
            @Override public void run() {
                slice.onBackPressed();
            }
        });
    }
}
